/**
* 版权声明：厦门睿商网络科技有限公司 版权所有 违者必究
* 日    期：2020-05-08
*/
package com.rzico.basics.controller.admin;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.rzico.annotation.Log;
import com.rzico.base.BaseController;
import com.rzico.base.CommResult;
import com.rzico.basics.entity.CompanyCategory;
import com.rzico.basics.entity.Enterprise;
import com.rzico.basics.model.CategoryCompanyVo;
import com.rzico.basics.service.CompanyCategoryService;
import com.rzico.basics.service.CompanyService;
import com.rzico.basics.service.EnterpriseService;
import com.rzico.entity.PageResult;
import com.rzico.entity.Pageable;
import com.rzico.util.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 企业分类控制层
 *
 * @author Rzico Boot
 * @version 1.0
 * @date 2020-05-08
 */
@Api(description = "企业分类接口")
@RestController
@RequestMapping("/admin/companyCategory")
public class CompanyCategoryAdminController extends BaseController {

    @Autowired
    private CompanyCategoryService companyCategoryService;

    @Autowired
    private EnterpriseService enterpriseService;

    @Autowired
    private CompanyService companyService;

    /**
     * 分页查询企业分类
     *
     * @return
    */
    @ApiOperation("分页查询企业分类")
    @GetMapping("/list")
    @ApiImplicitParams({@ApiImplicitParam(name = "id", value = "分类ID", dataType = "Long", paramType = "query")})
    public CommResult<CompanyCategory> list(Long id) {
        Map<String, Object> params = new HashMap<String, Object>();
        Enterprise enterprise = enterpriseService.getCurrent();
        if (enterprise!=null) {
            params.put("enterpriseId", enterprise.getId());
        } else {
            return CommResult.error("没有开通企业");
        }
        if (null != id){
            params.put("treePath", "," + id + ",");
        }

        params.put("sortField", "tree_path");
        params.put("sortType", "asc");

        List<CompanyCategory> list = companyCategoryService.selectTree(params);
        return CommResult.success(list);
    }

    /**
     * 查询单条企业分类
     *
     * @return
    */
    @ApiOperation("查询单条企业分类")
    @GetMapping("/find/{id}")
    public CommResult<CompanyCategory> find(@PathVariable String id) {
        CompanyCategory result = companyCategoryService.findById(id);

        Map<String,Object> data = new HashMap<>();
        data.put("companyCategory",result);
        return CommResult.success(data);

    }

    /**
     * 保存企业分类
     *
     * @param companyCategory
     * @return
     */
    @ApiOperation("保存企业分类")
    @PostMapping("/save")
    public CommResult<CompanyCategory> save(@RequestBody CompanyCategory companyCategory) {
        Enterprise enterprise = enterpriseService.getCurrent();
        if (enterprise!=null) {
            companyCategory.setEnterpriseId(enterprise.getId());
        } else {
            return CommResult.error("没有开通企业");
        }
        if (null == companyCategory.getIsShow()){
            companyCategory.setIsShow(true);
        }

        int affectCount = companyCategoryService.insertUseGeneratedKeys(companyCategory);
        if (affectCount <= 0) {
            return CommResult.error();
        }
        CompanyCategory result = companyCategoryService.findById(companyCategory.getId());
        return CommResult.success(result);

    }


    /**
     * 批量删除企业分类
     *
     * @param ids
     * @return
     */
    @Log(desc = "批量删除企业分类", type = Log.LOG_TYPE.DEL)
    @ApiOperation("批量删除企业分类,ids用逗号拼接")
    @PostMapping("/del/{ids}")
    public CommResult<CompanyCategory> del(@PathVariable String ids) {
        Enterprise enterprise = enterpriseService.getCurrent();
        if (null == enterprise) {
            return CommResult.error("没有开通企业");
        }
        Long enterpriseId = enterprise.getId();
        String[] allId = ids.split(",");
        for (String id : allId) {
            Map<String, Object> params = new HashMap<String, Object>();
            params.put("companyCategoryId", id);
            params.put("enterpriseId", enterpriseId);
            int num = companyService.countCompanyCategoryNum(params);
            if (0 < num) {
                return CommResult.error("当前分类被引用，不可删除");
            }
        }
        companyCategoryService.deleteByIds(ids.split(","));
        return CommResult.success();
    }

    /**
     * 更新企业分类
     *
     * @param companyCategory
     * @return
     */
    @Log(desc = "更新企业分类", type = Log.LOG_TYPE.UPDATE)
    @ApiOperation("更新企业分类")
    @PostMapping("/update")
    public CommResult<CompanyCategory> update(@RequestBody CompanyCategory companyCategory) {
        Enterprise enterprise = enterpriseService.getCurrent();
        if (enterprise!=null) {
            companyCategory.setEnterpriseId(enterprise.getId());
        } else {
            return CommResult.error("没有开通企业");
        }

        int affectCount = companyCategoryService.updateByPrimaryKeySelective(companyCategory);
        if (affectCount <= 0){
            return CommResult.error();
        }
        CompanyCategory result = companyCategoryService.findById(companyCategory.getId());
        return CommResult.success(result);
    }

}
